//
//  NSCityController.m
//  The Night Shift
//
//  Created by 张城 on 16/7/5.
//  Copyright © 2016年 张城. All rights reserved.
//

#import "NSCityController.h"
#import "NetWorkTool.h"
#import "WeatherView_Model.h"


@interface NSCityController ()<UITableViewDelegate,UITableViewDataSource>

//@property (nonatomic,strong) NSArray * dataArray;

@property (nonatomic,strong) UITableView * tableView;
@end

@implementation NSCityController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"当前城市位置";
    // Do any additional setup after loading the view.
    [self.view addSubview:self.tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    NSCityModel * model = self.cityArray;
//    NSLog(@"");
    return self.cityArray.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * ss = @"123456";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ss];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ss];
    }
    cell.textLabel.text = self.cityArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString * cityName = self.cityArray[indexPath.row];
    
//    [NSWeatherModel getWeatherWithCity:model.cityName];
    [WeatherView_Model weatherWithCurrentCity:cityName finishBlock:^(WeatherView_Model *weatherModel) {
        
        NSMutableDictionary * mudic = [NSMutableDictionary dictionary];
        
        [mudic setDictionary:@{@"city":cityName}];
        
        //保存选中城市到偏好设置
        NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:cityName forKey:@"city"];
        [defaults synchronize];
//        NSString * string = [defaults objectForKey:@"city"];

        NSNotification *notification =[NSNotification notificationWithName:@"tongzhi" object:nil userInfo:mudic];
        //通过通知中心发送通知
        [[NSNotificationCenter defaultCenter] postNotification:notification];
        
    } errorBlock:^{
        
    }];
    [self.navigationController popToRootViewControllerAnimated:YES];
}


- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

@end
